﻿using Itfort.Net;
using Newtonsoft.Json;
using QConnectSDK;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Web;
using Users.SnsConnect;

namespace Users.SnsConnect
{
	/// <summary>
	/// 
	/// </summary>
	public class OAuth2Utils
	{
		static OAuth2Utils()
		{
			Taobao_OAuth2Params = ParseOAuth2Param("taobao_");
			Weibo_OAuth2Params = ParseOAuth2Param("weibo_");
			Qzone_OAuth2Params = ParseOAuth2Param("qzone_");
			Renren_OAuth2Params = ParseOAuth2Param("renren_");
		}

		/// <summary>
		/// 
		/// </summary>
		public static OAuth2Params Taobao_OAuth2Params { get; set; }

		/// <summary>
		/// 
		/// </summary>
		public static OAuth2Params Weibo_OAuth2Params { get; set; }

		/// <summary>
		/// 
		/// </summary>
		public static OAuth2Params Qzone_OAuth2Params { get; set; }

		/// <summary>
		/// 
		/// </summary>
		public static OAuth2Params Renren_OAuth2Params { get; set; }

		static OAuth2Params ParseOAuth2Param(string prefix)
		{
			OAuth2Params oAuth2Params = new OAuth2Params();

			oAuth2Params.client_id = ConfigurationManager.AppSettings[prefix + "client_id"];
			oAuth2Params.client_secret = ConfigurationManager.AppSettings[prefix + "client_secret"];
			oAuth2Params.redirect_uri = ConfigurationManager.AppSettings[prefix + "redirect_uri"];
			oAuth2Params.auth_uri = ConfigurationManager.AppSettings[prefix + "auth_uri"];
			oAuth2Params.token_uri = ConfigurationManager.AppSettings[prefix + "token_uri"];

			return oAuth2Params;
		}

		/// <summary>
		/// 创建一个OAuth2Client
		/// </summary>
		/// <param name="oAuth2Params">OAuth2参数</param>
		/// <returns></returns>
		public static OAuth2Client CreateOAuth2Client(OAuth2Params oAuth2Params)
		{
			OAuth2Client oAuth2Client = new OAuth2Client();

			oAuth2Client.client_id = oAuth2Params.client_id;
			oAuth2Client.client_secret = oAuth2Params.client_secret;
			oAuth2Client.redirect_uri = oAuth2Params.redirect_uri;
			oAuth2Client.auth_uri = oAuth2Params.auth_uri;
			oAuth2Client.token_uri = oAuth2Params.token_uri;

			return oAuth2Client;
		}

		/// <summary>
		/// 创建一个OAuth2Client
		/// </summary>
		/// <param name="oAuth2Params">OAuth2参数</param>
		/// <param name="redirectUri">覆盖oAuth2Params中的redirect_uri</param>
		/// <returns></returns>
		public static OAuth2Client CreateOAuth2Client(OAuth2Params oAuth2Params, string redirectUri)
		{
			OAuth2Client oAuth2Client = new OAuth2Client();

			oAuth2Client.client_id = oAuth2Params.client_id;
			oAuth2Client.client_secret = oAuth2Params.client_secret;
			oAuth2Client.redirect_uri = redirectUri;
			oAuth2Client.auth_uri = oAuth2Params.auth_uri;
			oAuth2Client.token_uri = oAuth2Params.token_uri;

			return oAuth2Client;
		}

		/// <summary>
		/// 创建一个OAuth2Client
		/// </summary>
		/// <param name="oAuth2Params">OAuth2参数</param>
		/// <param name="code">授权码</param>
		/// <returns></returns>
		public static OAuth2Client CreateOAuth2Client_Token(OAuth2Params oAuth2Params, string code)
		{
			OAuth2Client oAuth2Client = CreateOAuth2Client(oAuth2Params);
			oAuth2Client.code = code;
			return oAuth2Client;
		}

		/// <summary>
		/// 交换淘宝账户信息
		/// </summary>
		/// <param name="code"></param>
		/// <returns></returns>
		public static SnsAccountInfo ExchangeTaobao(string code)
		{
			OAuth2Client oAuth2Client = OAuth2Utils.CreateOAuth2Client_Token(OAuth2Utils.Taobao_OAuth2Params, code);
			string json = oAuth2Client.ExchangeAccessToken();
			TaobaoToken token = JsonConvert.DeserializeObject<TaobaoToken>(json);
			token.taobao_user_nick = HttpUtility.UrlDecode(token.taobao_user_nick);

			SnsAccountInfo snsAccountInfo = new SnsAccountInfo(token);
			snsAccountInfo.SnsType = SnsType.Taobao;
			snsAccountInfo.SnsUserId = token.taobao_user_id;
			snsAccountInfo.SnsNickname = token.taobao_user_nick;

			return snsAccountInfo;
		}

		/// <summary>
		/// 交换微博账户信息
		/// </summary>
		/// <param name="code"></param>
		/// <returns></returns>
		public static SnsAccountInfo ExchangeWeibo(string code)
		{
			OAuth2Client oAuth2Client = OAuth2Utils.CreateOAuth2Client_Token(OAuth2Utils.Weibo_OAuth2Params, code);
			string json = oAuth2Client.ExchangeAccessToken();
			WeiboToken token = JsonConvert.DeserializeObject<WeiboToken>(json);
			string url = "https://api.weibo.com/2/users/show.json?access_token=" + token.access_token + "&uid=" + token.uid;
			string userJson = HttpClient.RequestContent(new Uri(url), "GET", Encoding.UTF8);
			WeiboUserInfo wbUser = JsonConvert.DeserializeObject<WeiboUserInfo>(userJson);

			SnsAccountInfo snsAccountInfo = new SnsAccountInfo(token);
			snsAccountInfo.SnsType = SnsType.Weibo;
			snsAccountInfo.SnsUserId = wbUser.id.ToString();
			snsAccountInfo.SnsNickname = wbUser.screen_name;

			return snsAccountInfo;
		}

		/// <summary>
		/// 交换Qzone账户信息
		/// </summary>
		/// <param name="code"></param>
		/// <returns></returns>
		public static SnsAccountInfo ExchangeQzone(string code)
		{
			// 交换AccessToken并获得QZONE资料
			string state = Guid.NewGuid().ToString().Replace("-", "");
			QOpenClient qOpenClinet = new QOpenClient(code, state);
			var token = qOpenClinet.OAuthToken;
			var qUser = qOpenClinet.GetCurrentUser();

			SnsAccountInfo snsAccountInfo = new SnsAccountInfo();
			snsAccountInfo.access_token = token.AccessToken;
			snsAccountInfo.expires_in = token.ExpiresAt;
			snsAccountInfo.SnsType = SnsType.Qzone;
			snsAccountInfo.SnsUserId = token.OpenId;
			snsAccountInfo.SnsNickname = qUser.Nickname;

			return snsAccountInfo;
		}

		/// <summary>
		/// 交换人人账户信息
		/// </summary>
		/// <param name="code"></param>
		/// <returns></returns>
		public static SnsAccountInfo ExchangeRenRen(string code)
		{
			OAuth2Client oAuth2Client = OAuth2Utils.CreateOAuth2Client_Token(OAuth2Utils.Renren_OAuth2Params, code);
			string json = oAuth2Client.ExchangeAccessToken();
			RenrenToken token = Newtonsoft.Json.JsonConvert.DeserializeObject<RenrenToken>(json);

			SnsAccountInfo snsAccountInfo = new SnsAccountInfo(token);
			snsAccountInfo.SnsType = SnsType.Renren;
			snsAccountInfo.SnsUserId = token.user.id.ToString();
			snsAccountInfo.SnsNickname = token.user.name;

			return snsAccountInfo;
		}
	}

	/// <summary>
	/// 
	/// </summary>
	public class OAuth2Params
	{
		/// <summary>
		/// 
		/// </summary>
		public string client_id;

		/// <summary>
		/// 
		/// </summary>
		public string client_secret;

		/// <summary>
		/// 
		/// </summary>
		public string redirect_uri;

		/// <summary>
		/// 
		/// </summary>
		public string auth_uri;

		/// <summary>
		/// 
		/// </summary>
		public string token_uri;
	}
}